﻿using StarPizza.Model.BaseModel;
using StarPizza.Model.Core;
using StarPizza.ViewModel.Core;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StarPizza.ViewModel.ManagerSystem
{
    class ToppingAdministrator : IngredientAdministrator
    {
        ToppingLister toppingLister;
        public ToppingLister ToppingLister { get { return toppingLister; } }

        private ToppingVM ActualToppingVM
        {
            get { return toppingLister.SelectedIngredientVM; }
        }
        protected override int ActualCost
        {
            get 
            {
                if (ActualToppingVM != null)
                    return ActualToppingVM.Cost;
                else
                    return 0;
            }
        }
        protected override int ActualPrice
        {
            get 
            {
                if (ActualToppingVM != null)
                    return ActualToppingVM.Price;
                else
                    return 0;
            }
        }

        public string Name
        {
            get 
            {
                if (ActualToppingVM != null)
                    return ActualToppingVM.Name;
                else
                    return "";
            }
        }

        private string newName;
        public string NewName
        {
            get { return newName; }
            set
            {
                if (value != newName)
                {
                    newName = value;
                    OnPropertyChanged("NewName");
                }
            }
        }

        public ToppingAdministrator(StarDataDataContext db, StockHandler stockHandler, ToppingLister toppingLister)
            : base(db, stockHandler)
        {
            this.toppingLister = toppingLister;
            toppingLister.IngredientVMSelected += toppingLister_ToppingVMSelected;
            IngredientSelected();
        }
        void toppingLister_ToppingVMSelected(ToppingVM selectedToppingVM)
        {
            IngredientSelected();
            OnPropertyChanged("Name");
        }
        public void UpdateTopping()
        {
            ActualToppingVM.Cost = EditedCost;
            ActualToppingVM.Price = EditedPrice;
            ActualToppingVM.UpdateModel();
            Utility.TrySubmitChanges(db);
            
            IngredientUpdated();
        }
        public void CreateTopping()
        {
            stockHandler.CreateTopping(NewName, NewCost, NewPrice);
        }
        public void SupplyTopping()
        {
            stockHandler.SupplyTopping(ActualToppingVM.WrappedTopping, SupplyAmount);
        }
    }
}
